import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import eslint from "vite-plugin-eslint";
import Inspect from "vite-plugin-inspect"; // 查看页面编译之后的结果
import autoImport from "unplugin-auto-import/vite";
import { resolve } from "path";
import { esbuildPluginMonacoEditorNls, Languages } from "./plugin";
const zh_CN = require("./plugin/main.i18n.json");

export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd());
  return {
    base: env.VITE_APP_BASE_URL,
    resolve: {
      alias: {
        "@": resolve(__dirname, "src"),
      },
    },
    optimizeDeps: {
      /** vite 版本需要大于等于2.3.0 */
      esbuildOptions: {
        plugins: [
          esbuildPluginMonacoEditorNls({
            locale: Languages.zh_hans,
            localeData: zh_CN.contents,
          }),
        ],
      },
    },
    plugins: [
      vue(),
      eslint(),
      Inspect(),
      autoImport({
        dts: "src/auto-import.d.ts", // 解决自动引入未定义警告信息
        imports: ["vue", "vue-router", "pinia"],
        eslintrc: {
          enabled: true, // 1、改为true用于生成eslint配置。2、生成后改回false，避免重复生成消耗
        },
      }),
    ],
    define: {
      __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: false, // 解决一个warn
    },
    // 启动服务配置
    server: {
      host: "0.0.0.0",
      port: parseInt(env.VITE_APP_PORT),
      open: false,
      https: false,
      cors: true,
      proxy: {
        "/dev-api": {
          target: "http://localhost:8000/w",
          changeOrigin: true,
          rewrite: (p) => p.replace(/^\/dev-api/, ""),
        },
      },
    },
    // 生产环境打包配置
    // 去除 console debugger
    build: {
      outDir: "dist",
      minify: "terser",
      sourcemap: false,
      reportCompressedSize: false, // 禁用 gzip 压缩大小报告，可略微减少打包时间
      chunkSizeWarningLimit: 2000, // 规定触发警告的 chunk 大小
      terserOptions: {
        compress: {
          drop_console: true,
          drop_debugger: true,
        },
      },
      rollupOptions: {
        output: {
          // Static resource classification and packaging
          chunkFileNames: "assets/js/[name]-[hash].js",
          entryFileNames: "assets/js/[name]-[hash].js",
          assetFileNames: "assets/[ext]/[name]-[hash].[ext]",
        },
      },
    },
  };
});
